define(function(require, exports, module) {

	var $ = require('jquery');
	var List = require('../util/list');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-json/2.4.0/jquery.json.js');
	require('seajs/seajs/2.1.1/sea.js');
	require('jbox');
	
	require('jquery/jquery-file-upload/8.8.5/vendor/jquery.ui.widget.js');
	require('jquery/jquery-file-upload/8.8.5/jquery.iframe-transport.js');
	require('jquery/jquery-file-upload/8.8.5/jquery.fileupload.js');
	require('../util/charCount');

	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : ''
	};
	
	var submit_openid ;
	var uid = $("input[name=uid]").attr("value");

	//加载标签
	var url = $CONFIG.base_url + '/api/configure/get';
	$.get(url,function(data){
		$("#itemFirstname").text(data.itemFirstname);
		$("#itemSecname").text(data.itemSecname);
		$("#itemFname").text(data.itemFname);
	});

	//判断项目标签
	exports.tags=function(o){
		
		if($(o).val() == '5'){
			$("#tags").attr({style:"display:block;"});
			$("#hot").attr({style:"display:block;"});
//			$("#needrec").attr({style:"display:block;"});
//			$("#needatt").attr({style:"display:block;"});
//			$("#needsup").attr({style:"display:block;"});
			$("#gold").attr({style:"display:block;"});
			$("#small").attr({style:"display:block;"});
		}else {
			$("#tags").attr({style:"display:none;"});
			$("#hot").attr({style:"display:none;"});
//			$("#needrec").attr({style:"display:none;"});
			$("#needatt").attr({style:"display:none;"});
			$("#needsup").attr({style:"display:none;"});
			$("#gold").attr({style:"display:none;"});
			$("#small").attr({style:"display:none;"});
		}
		
/*		$("#tags").attr({style:"display:none;"});
		$("#hot").attr({style:"display:none;"});
//		$("#needrec").attr({style:"display:none;"});
		$("#needatt").attr({style:"display:none;"});
		$("#needsup").attr({style:"display:none;"});
		if($(o).val()=='2'){
			$("#tags").attr({style:"display:block;"});
//			$("#needrec").attr({style:"display:block;"});
		}else if($(o).val()=='3'){
			$("#tags").attr({style:"display:block;"});
			$("#hot").attr({style:"display:block;"});
//			$("#needrec").attr({style:"display:block;"});
			$("#needatt").attr({style:"display:block;"});
			$("#needsup").attr({style:"display:block;"});
		}else if($(o).val()=='4'){
			$("#tags").attr({style:"display:block;"});
			$("#hot").attr({style:"display:block;"});
//			$("#needrec").attr({style:"display:block;"});
			$("#needatt").attr({style:"display:block;"});
			$("#needsup").attr({style:"display:block;"});
		}else{
			$("#tags").attr({style:"display:none;"});
			$("#hot").attr({style:"display:none;"});
//			$("#needrec").attr({style:"display:none;"});
			$("#needatt").attr({style:"display:none;"});
			$("#needsup").attr({style:"display:none;"});
		}
		*/
		
	};
	
	exports.tags("#status");
	
	$("#status").change(function(){
		$("input[type=checkbox]").attr("checked",false);
		exports.tags("#status");
	});
	
/*	
	//获取修改前开放平台分类
	var getting = $.get($CONFIG.base_url + "/api/system/project/categorylist",{type:2});
	getting.done(function(data){
		$.each(data,function(key,value){
			var options = $('<option name="tags.open" id="'+value.id +'" name="' + value.categoryName +  '" >' + value.categoryName + '</option>');
			$("select[id=projectOpen]").append(options);
		})
		var openid = $("input[name=opencategory]").attr("id");
		$("select[id=projectOpen]").children("option[id=" + openid +"]").attr("selected","selected");
		submit_openid = $("select[id=projectOpen]").children("option:selected").attr("id");
	});
	
	//获取修改后开放平台分类
	$("select[id=projectOpen]").change(function(){
		submit_openid = $(this).children("option:selected").attr("id");
	});
*/	
	//获取修改前项目收益分类
	var getting = $.get($CONFIG.base_url + "/api/system/project/categorylist",{type:3});
	getting.done(function(data){
		$.each(data,function(key,value){
			var options = $('<option name="tags.benefit" id="'+value.id +'" name="' + value.categoryName +  '" >' + value.categoryName + '</option>');
			$("select[id=projectBenefit]").append(options);
		})
		var benefitid = $("input[name=benefitcategory]").attr("id");
		$("select[id=projectBenefit]").children("option[id=" + benefitid +"]").attr("selected","selected");
		submit_benefitid = $("select[id=projectBenefit]").children("option:selected").attr("id");
	});
	
	//获取修改后项目收益分类
	$("select[id=projectBenefit]").change(function(){
		submit_benefitid = $(this).children("option:selected").attr("id");
	});
	
	
	//获取服务信息
	exports.productsList = function productsList(e) {

		var product = new List();
		product.options.template = "productList-template";
		product.options.container = "#productsList";
		product.options.links = {
			"list" : $CONFIG.base_url + '/api/project/product/list'
		};
		product.options.params = {
			"pid" : e,
			"y":1
		};
		product.load(function(){
		}
		);
	};
	var pro_id=$("input[name=id]").attr("value");
	exports.productsList(pro_id);
	
	//关闭服务 
	$("#productsList").on('click','button[name=close]',function(e){
	e.preventDefault();
	
	var productId = $(this).attr("data");
	var url = $CONFIG.base_url + '/api/project/product/close';
	var close = $.post(url,{"productId":productId});
	close.done(function(data){
		if(data){
			$.jBox.tip("关闭服务成功！");
			exports.productsList(pro_id);
		}else{
			$.jBox.tip("关闭服务失败！");
			exports.productsList(pro_id);
		}
	}).fail(function(){
		$.jBox.tip("Error！")
		exports.productsList(pro_id);
	})
	
	
	});
	
	//开启服务 
	$("#productsList").on('click','button[name=open]',function(e){
	e.preventDefault();
	
	var productId = $(this).attr("data");
	var url = $CONFIG.base_url + '/api/project/product/open';
	var close = $.post(url,{"productId":productId});
	close.done(function(data){
		if(data){
			$.jBox.tip("开启服务成功！");
			exports.productsList(pro_id);
		}else{
			$.jBox.tip("开启服务失败！");
			exports.productsList(pro_id);
		}
	}).fail(function(){
		$.jBox.tip("Error！")
		exports.productsList(pro_id);
	})
	
	
	});
	
	
	// 上传图片
	exports.fileUpload = function fileUpload(e, v, a, s) {
		$("#" + a).fileupload(
				{
					url : $CONFIG.base_url + e,
					dataType : 'json',
					formData : {
						uid : uid
						},
				 add: function (e, data) {
				        var goUpload = true;
				        var uploadFile = data.files[0];
				        if (!(/\.(gif|jpg|jpeg|png)$/i).test(uploadFile.name)) {
				        	$.jBox.tip('亲~请选择正确的图片格式');
				            goUpload = false;
				        }
				        if (uploadFile.size > 1048576) { // 1mb
				        	$.jBox.tip('亲~图片太大了，最大不超过1mb哦！');
				            goUpload = false;
				        }
				        if (goUpload == true) {
				            data.submit();
				        }
				    },
				    progressall : function(e, data) {
						var progress = parseInt(data.loaded / data.total * 100,10);
						$("#loading-load").show();
						$("#loading-load").text(progress + '%');
					},
					done : function(e, data) {
						 var result = data.result[0].body ? data.result[0].body.innerHTML : data.result;
						$.each(result, function(index, file) {
							var html = '<img src="' + $CONFIG.base_url
									+ '/common/attachment/download/' + file.id
									+ '/250x250"/>';
							if (s == "file-input") {
								$(".thumbnail_prepic").empty().append(html);
								$('#' + v).val(file.id);
								$('.' + v + "-name").text(file.fileName);
								$("#loading-load").hide();
							} else {
								$('#' + s).empty().append(html);
								$('#' + v).val(file.id);
								$("#loading-load").hide();
							}
						});
					}
				});
	};
	
	$("#goodsSetgoodAdit").on("click", ".upload_f", function() {
		var d = $(this).attr("data");
		var v = "photoId" + d;
		var a = $(this).attr("id");
		var b = "file-preview" + d;
		exports.fileUpload("/common/attachment/upload/multi", v, a, b);
	});
	
	// 添加服务服务信息
	var gid = '';
	$("#add-submit").click(function() {
		if($("input[id=goodsName]").val()==""){
			$.jBox.tip("亲~服务名称不能为空哦!");
			return;
		}

		var vali='';
		if($("input[id=photoId]").val()==""){
			$.jBox.tip("亲~您还没有上传服务图片！");
			return;
		}
		if($("textarea[id=goodsText]").val()==""){
			$.jBox.tip("亲~服务介绍不能为空哦!");
			return;
		}
		if($("input[name=price]").val()=="0"){
			$.jBox.tip("亲~服务价值不能为0哦!");
			return;
			return;
		}else if($("input[name=price]").val().charAt(0)=="0"){
			$.jBox.tip("亲～服务价值格式不对！");
			return;
		} 
		/*if($("input[name=ammount]").val()=="0"){
			$.jBox.tip("亲~服务个数不能为0哦!");
			return;
			return;
		}else if($("input[name=ammount]").val().charAt(0)=="0"){
			$.jBox.tip("亲～服务个数格式不对！");
			return;
		}*/

		if(vali!=""&&vali!="photoId"){
			switch(vali){
			case "price":
				$.jBox.tip("亲~服务收费标准不能为空哦!");
				break;
			/*case "ammount":
				$.jBox.tip("亲~服务个数不能为空哦!");
				break;*/
			default:
				break;
			}
			return;
		}
		
		
		var postData = form2js('goodsSetgoodAdit', '.', true);
		
		var url= $CONFIG.base_url+"/api/project/product/add";
		postData["projectId"] =pro_id;
		Data=$.toJSON(postData);
		$.ajax({
			url : $CONFIG.base_url + "/api/project/product/add",
			type : "POST",
			data : Data,
			// dataType : "json",
			contentType : "application/json",
			success : function(data) {
				$.jBox.tip("添加成功！");
				$("#upModal table").empty();
				$("#upModal").modal("hide");
				exports.productsList(pro_id);
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		});
	});
	//提交项目信息
	$('.add-submit').click(function(){
		var postData = form2js('update-form', '.', true);
		var tags=postData.tags;
		if(tags!=null){
			var tagstr="";
			$.each(tags, function(item){	
				tagstr=tagstr+item+",";
			});	
			postData.tags=tagstr;
		}
		var flag=0;
		$("#project-tags input[type='checkbox']").each(function(){
			if($(this).prop("checked")){
				flag=1;
			}
		});
		if(flag==0){
			postData["tags.needsup"]="null";
		}
		delete postData.projectOpen;
		//postData["projectOpen"] ={"id": submit_openid};
		postData["projectBenefit"] ={"id": submit_benefitid};
		//console.log(postData);
		$.ajax({
			url : $CONFIG.base_url + "/api/admin/project/update",
			type : "POST",
			data : $.toJSON(postData),
			dataType : "json",
			contentType : "application/json",
			success : function() {
				$.jBox.tip("更新服务信息成功！");
				setTimeout(function(){window.location.href=$CONFIG.base_url + '/'+$CONFIG.admin_url+'/manage/project';},1000);
			},
			error : function() {
				$.jBox.tip("更新服务信息失败！");
			}
		});
	});
});